package com.android.baseconfig.common.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author lei
 * @description SecurityUtils
 * @date 2018/5/5
 */
public class SecurityUtils {
    private static final String TAG = SecurityUtils.class.getName();
    public static final String GBK_ENCODING = "UTF-8";
    public static final String KEY_AES = "09e8edf6926ed3ds";

    public static String encryptApp(String value, String secretKey) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(GBK_ENCODING), "AES");
            byte[] buffer;
            Arrays.fill(buffer = new byte[16], (byte) 0);
            byte[] valueBytes = value.getBytes(GBK_ENCODING);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(buffer);
            Cipher cipher;
            (cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")).init(1, secretKeySpec, ivParameterSpec);
            return byte2String(Base64.encode(cipher.doFinal(valueBytes), 2));
        } catch (InvalidKeyException e) {
            Logc.e(TAG, "", e);
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e1) {
            Logc.d(TAG, "encryptApp error");
        } catch (IllegalArgumentException e7) {
            Logc.e(TAG, "Key may be lost.");
        }

        return null;
    }

    public static String decryptApp(String key, String secretKey) {
        if (key != null && secretKey != null) {
            try {
                SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(GBK_ENCODING), "AES");
                byte[] buffer;
                Arrays.fill(buffer = new byte[16], (byte) 0);
                IvParameterSpec ivParameterSpec = new IvParameterSpec(buffer);
                byte[] bytes = Base64.decode(key.getBytes(GBK_ENCODING), 2);
                Cipher cipher;
                (cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")).init(2, secretKeySpec, ivParameterSpec);
                return byte2String(cipher.doFinal(bytes));
            } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
                    | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
                Logc.d(TAG, "decryptApp error");
            } catch (UnsupportedEncodingException e6) {
                Logc.d(TAG, "", e6);
            } catch (IllegalArgumentException e7) {
                Logc.e(TAG, "Key may be lost.");
            }

            return null;
        } else {
            return null;
        }
    }

    private static String byte2String(final byte[] array) {
        return new String(array, Charset.forName(GBK_ENCODING));
    }
}
